<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title>简历上传</title>
    <link rel="shortcut icon" href="../../image/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="../../css/main.css" media="all">
</head>
<body>
<div class="layui-layout layui-layout-admin">
    <div class="layui-container">
        <div class="layui-card" style="margin-top: 15px">
            <div class="layui-card-header"><b style="font-size: 18px">添加简历附件</b></div>
            <div class="layui-card-body">
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button type="button" class="layui-btn" id="resume">
                            <i class="layui-icon">&#xe67c;</i>上传简历附件
                        </button>
                    </div>
                </div>
            </div>
        </div>
        <table id="stuResume" lay-filter="stuResume"></table>
    </div>
</div>
<script src="../../libs/layui/layui.js"></script>
<script src="../../js/model.js"></script>
<script>
    layui.use(['layer', 'table', 'upload', 'jquery', 'okLayer', 'okUtils'], function () {
        let layer = layui.layer;
        let table = layui.table;
        let upload = layui.upload;
        let $ = layui.jquery;
        let okLayer = layui.okLayer;
        let okUtils = layui.okUtils;

        // 获取contentText
        const ctx = function () {
            let pathName = document.location.pathname;
            let index = pathName.substr(1).indexOf("/");
            let result = pathName.substr(0, index + 1);
            return result;
        }();
        // 获取enterprise
        var student = JSON.parse(window.sessionStorage.getItem("student"));


        //文件上传
        var uploadInst = upload.render({
            elem: '#resume' //绑定元素
            , url: ctx + '/upload/resumeUpload' //上传接口
            , accept: 'file'
            , acceptMime: 'application/pdf'
            , exts: 'pdf'//只允许上传excel文件
            , data: {diridentity: student.stuid}
            , done: function (res) {
                if (res.code == 200) {
                    savetodatabase(res.data.src);
                } else {
                    layer.msg("简历上传失败", {icon: 7})
                }
            }
            , error: function () {
                //请求异常回调
            }
        });

        //上传到数据库保存
        function savetodatabase(imagepath) {
            var index = imagepath.lastIndexOf("/");
            var resumename = imagepath.substring(index + 1);
            var resume = new Resume(null, resumename, imagepath, null, student);
            $.ajax({
                url: ctx + '/ygssecs/addresume',
                type: 'post',
                data: {resumejson: JSON.stringify(resume)},
                dataType: 'json',
                beforeSend: function () {
                    layer.load()
                }
            }).done(function (res) {
                layer.closeAll();
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                        location.reload();
                    })
                } else {
                    layer.msg(res.msg, {icon: 2, time: 3000}, function () {
                        location.reload();
                    })
                }
            }).fail(function (error) {
                console.log(error)
            })
        }

        //渲染数据
        var resumetable = table.render({
            elem: '#stuResume'
            , width: 505
            , url: ctx + '/ygssecs/getresumelistbystuid' //数据接口
            , page: false //开启分页
            , where: {stuid: student.stuid}
            , response: {
                statusCode: 200 //规定成功的状态码，默认：0
            }
            , cols: [[ //表头
                {field: '', title: '序号', type: 'numbers', width: 80, align: "center", fixed: 'left'}
                , {field: 'resumeid', title: '简历编号', width: 150, align: "center"}
                , {field: 'resumename', title: '简历名', width: 150, align: "center"}
                , {field: 'resumepath', title: '文件地址', width: 280, hide: true}
                , {title: "操作", width: 120, align: "center", templet: "#operationTpl"}
            ]], done: function (res, curr, count) {
                console.log(res, curr, count);
            }
        });

        table.on("tool(stuResume)", function (obj) {
            let data = obj.data;
            switch (obj.event) {
                case "viewresume":
                    viewresume(data.resumepath);
                    break;
                case "del":
                    del(data.resumeid);
                    break;
            }
        });


        function viewresume(data) {
            okLayer.open("查看简历", data, "90%", "90%", null, null)
        }

        function del(resumeID) {
            okLayer.confirm("确定要删除吗？", function () {
                okUtils.ajax("/ygssecs/delresume", "post", {resumeid: resumeID}, true).done(function (response) {
                    okLayer.greenTickMsg(response.msg, function () {
                        resumetable.reload();
                    })
                }).fail(function (error) {
                    console.log(error)
                });
            })
        }
    });
</script>
<script type="text/html" id="operationTpl">
    <a href="javascript:" title="预览" lay-event="viewresume"><i class="layui-icon resume-fontsize">&#xe652;</i></a>
    <a href="javascript:" title="删除" lay-event="del"><i class="layui-icon resume-fontsize">&#xe640;</i></a>
</script>
</body>

</html>